<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FOHost</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-bottom: 10px;
            font-weight: bold;
        }
        input[type="text"] {
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 10px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button.yellow {
            background-color: #ffc107;
        }
        button.red {
            background-color: #dc3545;
        }
        button.green {
            background-color: #28a745;
        }
        button:hover {
            opacity: 0.8;
        }
        #history {
            margin-top: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        canvas {
            margin-top: 20px;
        }
        #notification {
            position: fixed;
            bottom: 20px;
            left: 20px;
            background-color: #333;
            color: white;
            padding: 10px;
            border-radius: 5px;
            display: none;
            z-index: 1000;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <div class="container">
        <h1>FOHost</h1>
        <form id="hostsForm">
            <label for="hostEntry">添加到 Hosts 文件:</label>
            <input type="text" id="hostEntry" name="hostEntry" placeholder="请输入 IP 地址和域名" required>
            <button type="submit" id="submitButton">提交</button>
        </form>
        <div id="history">
            <h2>流量出入</h2>
            <table id="historyTable">
                <thead>
                    <tr>
                        <th>历史条目</th>
                    </tr>
                </thead>
                <tbody id="historyList"></tbody>
            </table>
        </div>
        <canvas id="trafficChart" width="400" height="200"></canvas>
    </div>

    <div id="notification"></div>

    <script>
        const historyList = document.getElementById('historyList');
        const submitButton = document.getElementById('submitButton');
        const notification = document.getElementById('notification');
        const hostEntry = document.getElementById('hostEntry');
        let lastInput = '';

        const ctx = document.getElementById('trafficChart').getContext('2d');
        const trafficChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: [],
                datasets: [
                    {
                        label: '入流量',
                        borderColor: 'blue',
                        backgroundColor: 'rgba(0, 0, 255, 0.1)',
                        data: [],
                        fill: true,
                    },
                    {
                        label: '出流量',
                        borderColor: 'red',
                        backgroundColor: 'rgba(255, 0, 0, 0.1)',
                        data: [],
                        fill: true,
                    }
                ]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });

        let incomingTraffic = 0;
        let outgoingTraffic = 0;

        function updateTrafficData() {
            incomingTraffic += Math.floor(Math.random() * 10);
            outgoingTraffic += Math.floor(Math.random() * 10);

            const currentTime = new Date().toLocaleTimeString();
            trafficChart.data.labels.push(currentTime);
            trafficChart.data.datasets[0].data.push(incomingTraffic);
            trafficChart.data.datasets[1].data.push(outgoingTraffic);
            trafficChart.update();

            setTimeout(updateTrafficData, 1000);
        }

        updateTrafficData();

        function showNotification(message) {
            notification.textContent = message;
            notification.style.display = 'block';
            setTimeout(() => {
                notification.style.display = 'none';
            }, 3000);
        }

        document.getElementById('hostsForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const input = hostEntry.value;

            submitButton.textContent = '写入中';
            submitButton.classList.add('yellow');
            submitButton.disabled = true;
            hostEntry.style.display = 'none';

            const response = await fetch('/add', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `entry=${encodeURIComponent(input)}`
            });
            const message = await response.text();
            showNotification(message);

            const listItem = document.createElement('tr');
            listItem.innerHTML = `<td>${input}</td>`;
            historyList.appendChild(listItem);

            lastInput = input;

            submitButton.textContent = '停止';
            submitButton.classList.remove('yellow');
            submitButton.classList.add('red');
            submitButton.disabled = false;

            hostEntry.value = '';
            hostEntry.style.display = 'block';
        });

        submitButton.addEventListener('click', async () => {
            if (submitButton.textContent === '停止') {
                const entryToClear = historyList.lastChild.textContent;
                const response = await fetch('/clear', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `entry=${encodeURIComponent(entryToClear)}`
                });
                const message = await response.text();
                showNotification(message);
                historyList.removeChild(historyList.lastChild);

                submitButton.textContent = '提交';
                submitButton.classList.remove('red');
                submitButton.classList.add('green');
                submitButton.disabled = false;
            } else {
                const response = await fetch('/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `entry=${encodeURIComponent(lastInput)}`
                });
                const message = await response.text();
                showNotification(message);

                const listItem = document.createElement('tr');
                listItem.innerHTML = `<td>${lastInput}</td>`;
                historyList.appendChild(listItem);

                submitButton.textContent = '停止';
                submitButton.classList.remove('green');
                submitButton.classList.add('red');
                submitButton.disabled = false;
            }
        });
    </script>
</body>
</html>
